package CGIMan::FileList;

use Moose;

use Data::Dumper;
use List::MoreUtils qw(uniq);
use DateTime;
use Pod::Find qw(pod_find);
use File::Find;
use Log::Log4perl;

use CGIMan::File;
with 'MooseX::Log::Log4perl';

use vars qw/*name *dir *prune /;

=head2 file_list

An array ref contaning a list of CGIMan::File objects

=cut

has 'file_list' => (
    is      => 'rw',
    traits  => ['Array'],
    isa     => 'ArrayRef[CGIMan::File]',
    lazy    => 1,
    builder => '_get_file_list',
);

has 'pod_dirs' => (
    is      => 'rw',
    traits  => ['Array'],
    isa     => 'ArrayRef[Str]',
    default => sub { return [] },
    handles => {
        all_options    => 'elements',
        add_option     => 'push',
        map_options    => 'map',
        filter_options => 'grep',
        find_option    => 'first',
        get_option     => 'get',
        join_options   => 'join',
        count_options  => 'count',
        has_options    => 'count',
        has_no_options => 'is_empty',
        sorted_options => 'sort',
    },
);

sub BUILDARGS {
    my $class  = shift;
    my %params = @_;

    #->log->debug("START");
    # Strip out directories from pod_dirs that don't exist.
    @{ $params{pod_dirs} } = grep { -d $_ } @{ $params{pod_dirs} };

    ### Set to just one dir for debug purposes.
    #$params{pod_dirs} = [qw|/usr/share/perl5|];

    #print STDERR ( 'Param dump: ' . Dumper(\%params));
    return \%params;
}

sub BUILD {
    my $self = shift;

    $self->log->trace( "BUILD start: " . Dumper( \@_ ) )
      if $self->log->is_trace;

    #warn "Build START";
    $self->update_file_list;

    #warn "POST BUILD";

    #warn "BUILD END: " . Dumper($self);
}

=head2 update_file_list

Returns an array of file list data.

=cut

sub update_file_list {
    my $self = shift;

    $self->file_list( $self->_get_file_list );
}

=head2 _get_file_list

Returns an array ref containing the list of full paths for each file
found.

=cut

sub _get_file_list {
    my $self = shift;
    my $log  = $self->log('_get_file_list');

    #/home/eberg/work/BergBrains-PerlDocViewer/lib/CGIMan/FileList.pm
    my @files;
    my @pod_dirs;
    my %pod_find_options = (
        -verbose => 0,    #$log->is_trace,
                          #-inc => 1,
    );
    my $pod_dirs = $self->pod_dirs();
    $log->info( "PD1: " . Dumper( $self->pod_dirs() ) );    # if $log->is_trace;
    my %pod_files = pod_find( \%pod_find_options, @$pod_dirs );

    $log->trace( "Pod files dump: " . Dumper( \%pod_files ) );

    foreach my $file_path ( keys %pod_files ) {
        $log->trace("Creating File obj for '$file_path'");
        next if $file_path =~ m/~$/;

        # Get last mod date
        #warn "File name: " . $File::Find::name;
        my (
            $dev,  $ino,   $mode,  $nlink, $uid,     $gid, $rdev,
            $size, $atime, $mtime, $ctime, $blksize, $blocks
        ) = stat($file_path);
        my $modified_date = DateTime->from_epoch( epoch => $mtime );

        #warn "About to push '$File::Find::name'";
        $self->log->info( "POD dirs: " . Dumper($pod_dirs) );
        # if $log->is_trace;
        my $file_obj = CGIMan::File->new(
            full_path     => $file_path,
            inc_dir_list  => $pod_dirs,
            last_modified => $modified_date,
            package_name  => $pod_files{$file_path},
        );
        $log->trace( "S " . Dumper($file_obj) );

        #warn "File obj dump: " . Dumper($file_obj);
        push @files, $file_obj;
    }

    #    $log->debug( "Samle File obj dump: " . Dumper( $files[2]));

    $log->debug( "Found pod file count: " . @files );
    $log->trace( "Pod files dump: " . Dumper( \@files ) );
    return \@files;
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;
